#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    QFile qss(":/white.qss");
    qss.open(QFile::ReadOnly);
    qApp->setStyleSheet(qss.readAll());
    qss.close();
    ui->setupUi(this);
    this->setFixedSize(570, 430);
    this->setWindowTitle("点餐");
    currentLineEdit = ui->lineEdit_2;
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_3_clicked()
{
    numberButtonfunc("3");
}


void Widget::on_pushButton_clicked()
{
   numberButtonfunc("1");
}

void Widget::on_pushButton_2_clicked()
{
    numberButtonfunc("2");
}

void Widget::on_pushButton_8_clicked()
{
    numberButtonfunc("8");
}

void Widget::on_pushButton_4_clicked()
{
    numberButtonfunc("4");
}

void Widget::on_pushButton_5_clicked()
{
    numberButtonfunc("5");
}

void Widget::on_pushButton_6_clicked()
{
    numberButtonfunc("6");
}

void Widget::on_pushButton_7_clicked()
{
    numberButtonfunc("7");
}

void Widget::on_pushButton_9_clicked()
{
    numberButtonfunc("9");
}

void Widget::on_pushButton_13_clicked()
{
    numberButtonfunc("0");
}

//void Widget::mousePressEvent(QMouseEvent *event)
//{
//    // 获取点击的小部件
//    QWidget *clickedWidget = QApplication::widgetAt(event->globalPos());

//    // 检查是否为 QLineEdit
//    QLineEdit *clickedLineEdit = qobject_cast<QLineEdit*>(clickedWidget);
//    if (clickedLineEdit) {
//        // 保存当前点击的 QLineEdit 指针
//        currentLineEdit = clickedLineEdit;
//    }

//    // 调用基类的事件处理函数
//    QWidget::mousePressEvent(event);
//}

void Widget::numberButtonfunc(QString a)
{
    if (currentLineEdit->text().isEmpty()) {
        currentLineEdit->setText(a);
        return;
    }


    QString id = currentLineEdit->text().append(a);


    currentLineEdit->setText(id);
}


//void Widget::numberButtonfunc(int a)
//{
//    qDebug()<<currentLineEdit->objectName();
//        if(currentLineEdit->text() == ""){
//            currentLineEdit->setText(QString("%1").arg(a));
//            return ;
//        }
//        double tableID = currentLineEdit->text().toDouble();
//        QString id;
//        if( 0 == tableID){
//            id = QString("%1").arg(a);
//        }else{
//            id = currentLineEdit->text().append(QString("%1").arg(a));
//        }
//        currentLineEdit->setText(id);


//}


void Widget::on_pushButton_12_clicked()
{
    ui->lineEdit->setText("");
}

void Widget::on_pushButton_10_clicked()
{
    int tableID = ui->lineEdit->text().toInt();
    QString ip = ui->lineEdit_2->text();
    int port = ui->lineEdit_3->text().toInt();
    Form1 *form = new Form1(tableID,ip,port);
    form->show();
}

//void Widget::on_pushButton_14_clicked()
//{
//    socket = new QTcpSocket;
//    QString ip = ui->lineEdit_2->text();
//    int port = ui->lineEdit_3->text().toInt();
//    // 连接服务器
//    socket->connectToHost(QHostAddress(QString(ip)), port);

//    // 连接成功时触发 connected 信号
//    connect(socket, &QTcpSocket::connected, [&]() {
//        // 连接成功，显示提示框
//        QMessageBox::information(nullptr, "连接成功", "成功连接到服务器");
//    });

//    // 连接失败时触发 errorOccurred 信号
//    connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), [&](QAbstractSocket::SocketError socketError) {
//        // 连接失败，显示错误提示框
//        QMessageBox::critical(nullptr, "连接失败", "连接错误: " + socket->errorString());
//    });

//}

void Widget::on_pushButton_14_clicked()
{
    ui->pushButton_14->setStyleSheet("QPushButton:checked { background-color: blue; }");
    ui->pushButton_15->setChecked(false);
    ui->pushButton_16->setChecked(false);
    currentLineEdit = ui->lineEdit_2;
    ui->pushButton_14->setChecked(true);
}

void Widget::on_pushButton_15_clicked()
{
    ui->pushButton_15->setStyleSheet("QPushButton:checked { background-color: blue; }");
    ui->pushButton_14->setChecked(false);
    ui->pushButton_16->setChecked(false);
    currentLineEdit = ui->lineEdit_3;
    ui->pushButton_15->setChecked(true);
}

void Widget::on_pushButton_16_clicked()
{
    ui->pushButton_16->setStyleSheet("QPushButton:checked { background-color: blue; }");
    ui->pushButton_15->setChecked(false);
    ui->pushButton_14->setChecked(false);
    currentLineEdit = ui->lineEdit;
    ui->pushButton_16->setChecked(true);
}

void Widget::on_pushButton_17_clicked()
{
    numberButtonfunc(".");
}

void Widget::on_pushButton_18_clicked()
{
    QString text = currentLineEdit->text();
    if (text.isEmpty())
        return;
    text = text.chopped(1);

    currentLineEdit->setText(text);
}
